package Bill.app.client;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.util.*;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Bill.util.Configuration;

public class Chat extends JFrame
    implements KeyListener, WindowListener, ActionListener, ListSelectionListener
{

    Chat(String expediteur, String destinataire, String message, String Etat, Hashtable Discussion)
        throws IOException
    {
        super((new StringBuilder("BZHMessenger 1.0.0   live  ")).append(expediteur).append(" <==========>").append(destinataire).toString());
        saisie = new JTextField();
        Message = new JList();
        this.expediteur = null;
        this.destinataire = null;
        this.Etat = "OPENED";
        model = new DefaultListModel();
        info = new JLabel();
        coulexpediteur = "000";
        couldestinataire = "000";
        ecriture = null;
        lecture = null;
        this.Discussion = null;
        Etatici = null;
        this.expediteur = expediteur;
        this.destinataire = destinataire;
        this.Discussion = Discussion;
        this.Etat = Etat;
        Message.addListSelectionListener(this);
        Message.setCellRenderer(new LabelListCellRendererM());
        Message.setFont(new Font("Serif", 1, 20));
        if(message.compareToIgnoreCase(" ") != 0 && message.compareToIgnoreCase("END") != 0 && message.compareToIgnoreCase("900") != 0 && message.compareToIgnoreCase("800") != 0 && message.compareToIgnoreCase("WIZZ") != 0)
        {
            StringTokenizer tt = new StringTokenizer(message, "=");
            String coul = tt.nextToken();
            message = tt.nextToken();
            JLabel label = new JLabel((new StringBuilder("<html><font color=#")).append(coul).append(">").append(this.destinataire).append(":").append(message).append("</font></html>").toString());
            label.setToolTipText((new StringBuilder()).append((new Date()).toLocaleString()).toString());
            label.setForeground(Color.CYAN);
            label.setFont(new Font("Serif", 1, 15));
            model.addElement(label);
            Message.setModel(model);
        }
        JMenuItem help=new JMenuItem("Racourcis");
        JMenu menu=new JMenu("aide?");
        JMenuBar bar=new JMenuBar();
        bar.add(menu);
        menu.add(help);
        setJMenuBar(bar);
        help.addActionListener(new ActionListener() {
        	
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				JOptionPane.showMessageDialog(Chat.this,"<html>"
						+ "<ul><li>alt +w :wizz</li>"
						+ "<li><b>ctrl+alt+C: choix de couleur</b></li></ul></html>","racourcis",JOptionPane.INFORMATION_MESSAGE);
			}
		});
        
        JScrollPane scroller = new JScrollPane(Message);
        scroller.setVerticalScrollBarPolicy(22);
        scroller.setHorizontalScrollBarPolicy(30);
        scroller.setPreferredSize(new Dimension(300, 150));
        scroller.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(""), BorderFactory.createEmptyBorder(1, 1, 1, 1)), scroller.getBorder()));
        setLayout(new BorderLayout());
        JPanel pan = new JPanel();
        add("Center", pan);
        pan.setLayout(new BorderLayout());
        pan.add("Center", scroller);
        Message.setVisible(true);
        pan.add("South", info);
        add("South", saisie);
        saisie.setToolTipText("<html>appuyer <b>control+alt+c</b> pour changer la couleur du texte</html>");
        saisie.setForeground(couleur);
        addWindowListener(this);
        saisie.addKeyListener(this);
        Font font = new Font("Sanserif", 1, 18);
        saisie.setFont(font);
        Message.setFont(font);
        setSize(500, 600);
        Dimension dimensionecran = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((dimensionecran.width - getWidth()) / 2 - 100, (dimensionecran.height - getHeight()) / 2);
        setVisible(true);
        setResizable(false);
        setIconImage((new ImageIcon("images.gif")).getImage());
          Configuration.loadConfiguration();
        Socket  tchat = new Socket(Configuration.IPserveur, Configuration.Config_portServeur);
        
        ecriture = new PrintWriter(new BufferedWriter(new OutputStreamWriter(tchat.getOutputStream())), true);
        lecture = new BufferedReader(new InputStreamReader(tchat.getInputStream()));
        String mess = (new StringBuilder("4=")).append(this.expediteur).append(":").append(this.destinataire).toString();
        ecriture.println(mess);
        new ThreadTchat1(lecture, Message, info, this.destinataire, Etat, Etatici, this.Discussion, model);
        new EstENTrainEcrie(saisie, expediteur, destinataire, ecriture);
    }

    public void keyPressed(KeyEvent a)
    {
        String mess = saisie.getText().trim();
        if(a.getKeyCode() == 10)
        {
            if(mess.compareToIgnoreCase("") != 0)
            {
                String coul = (new StringBuilder(String.valueOf(OXconverter.convOX(couleur.getRed())))).append(OXconverter.convOX(couleur.getGreen())).append(OXconverter.convOX(couleur.getBlue())).toString();
                coulexpediteur = coul;
                JLabel label = new JLabel((new StringBuilder("<html><font color=#")).append(coulexpediteur).append(">vous:").append(mess).append("</font></html>").toString());
                label.setForeground(couleur);
                label.setToolTipText((new StringBuilder()).append((new Date()).toLocaleString()).toString());
                label.setFont(new Font("Serif", 1, 15));
                model.addElement(label);
                Message.setModel(model);
                try
                {
                    mess = (new StringBuilder(String.valueOf(expediteur))).append(":").append(destinataire).append(":").append(coul).append("=").append(mess).toString();
                    ecriture.println(mess);
                }
                catch(Exception exception) { }
                saisie.setText("");
            }//alt +w :wizz ctrl+alt+C
        } else
        if(a.isAltDown() && (a.getKeyChar() == 'w' || a.getKeyChar() == 'W'))
        {
            mess = (new StringBuilder(String.valueOf(expediteur))).append(":").append(destinataire).append(":WIZZ").toString();
            ecriture.println( mess);
            new Wiz((Chat)Discussion.get(expediteur), 30, 2);
        } else
        if(a.isAltDown() && a.isControlDown() && (a.getKeyCode() == 67 || a.getKeyCode() == 99))
        {
            couleur = JColorChooser.showDialog(this, "choix de la couleur du texte \340 envoyer?", Color.red);
            saisie.setForeground(couleur);
        }
    }

    public void keyReleased(KeyEvent keyevent)
    {
    }

    public void keyTyped(KeyEvent keyevent)
    {
    }

    public void windowActivated(WindowEvent windowevent)
    {
    }

    public void windowClosed(WindowEvent arg0)
    {
        String mess = (new StringBuilder(String.valueOf(expediteur))).append(":").append(destinataire).append(":END").toString();
        ecriture.println(mess);
        if(Discussion.containsKey(destinataire))
            Discussion.remove(destinataire);
        System.out.println(Discussion);
        Etatici = "CLOSED";
    }

    public void windowClosing(WindowEvent arg0)
    {
        String mess = (new StringBuilder(String.valueOf(expediteur))).append(":").append(destinataire).append(":END").toString();
        ecriture.println(mess);
        if(Discussion.containsKey(destinataire))
            Discussion.remove(destinataire);
        Etatici = "CLOSED";
    }

    public void windowDeactivated(WindowEvent windowevent)
    {
    }

    public void windowDeiconified(WindowEvent windowevent)
    {
    }

    public void windowIconified(WindowEvent windowevent)
    {
    }

    public void windowOpened(WindowEvent windowevent)
    {
    }

    public void actionPerformed(ActionEvent actionevent)
    {
    }

    public void valueChanged(ListSelectionEvent listselectionevent)
    {
    }

    JTextField saisie;
    JList Message;
    String expediteur;
    String destinataire;
    String Etat;
    static Color couleur;
    DefaultListModel model;
    JLabel info;
    String coulexpediteur;
    String couldestinataire;
    PrintWriter ecriture;
    BufferedReader lecture;
    Hashtable Discussion;
    String Etatici;

    static 
    {
        couleur = Color.blue;
    }
}
